package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.NavigationDto;
import com.cardone.platform.authority.service.NavigationService;

/**
 * 导航
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/navigation/")
@Controller
public class NavigationController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 导航：删除
	 *
	 * @param deleteNavigation
	 *          导航对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final NavigationDto deleteNavigation) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(NavigationService.class).deleteByIds(deleteNavigation);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, NavigationController.log);
				}
			}
		};
	}

	/**
	 * 导航：分页查询
	 *
	 * @param findNavigation
	 *          导航对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final NavigationDto findNavigation, final Model model) {
		final NavigationDto navigation = ContextHolder.getBean(NavigationService.class).findById(NavigationDto.class, findNavigation);

		model.addAttribute("navigation", navigation);
	}

	/**
	 * 导航：编辑
	 *
	 * @param findNavigation
	 *          导航对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<NavigationDto>> findByIdJson(final NavigationDto findNavigation, final Model model) {
		return new Callable<ReturnDataDto<NavigationDto>>() {
			@Override
			public ReturnDataDto<NavigationDto> call() throws Exception {
				try {
					final NavigationDto navigation = ContextHolder.getBean(NavigationService.class).findById(NavigationDto.class, findNavigation);

					return ReturnDataUtils.newReturnData(navigation);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, NavigationController.log);
				}
			}
		};
	}

	/**
	 * 导航：查询
	 *
	 * @param findListNavigation
	 *          导航对象
	 * @return 导航对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<NavigationDto>>> findListByLikeCodeJson(final NavigationDto findListNavigation) {
		return new Callable<ReturnDataDto<List<NavigationDto>>>() {
			@Override
			public ReturnDataDto<List<NavigationDto>> call() throws Exception {
				try {
					final List<NavigationDto> navigationList = ContextHolder.getBean(NavigationService.class).findListByLikeCode(NavigationDto.class, findListNavigation);

					return ReturnDataUtils.newReturnData(navigationList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, NavigationController.log);
				}
			}
		};
	}

	/**
	 * 导航：分页查询
	 *
	 * @param paginationNavigation
	 *          导航对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<NavigationDto>>> paginationByLikeCodeJson(final NavigationDto paginationNavigation) {
		return new Callable<ReturnDataDto<PaginationDto<NavigationDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<NavigationDto>> call() throws Exception {
				try {
					final PaginationDto<NavigationDto> navigationPagination = ContextHolder.getBean(NavigationService.class).paginationByLikeCode(NavigationDto.class, paginationNavigation);

					return ReturnDataUtils.newReturnData(navigationPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, NavigationController.log);
				}
			}
		};
	}

	/**
	 * 导航：分页查询
	 *
	 * @param paginationNavigation
	 *          导航对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final NavigationDto paginationNavigation, final Model model) {
		final PaginationDto<NavigationDto> navigationPagination = ContextHolder.getBean(NavigationService.class).paginationByLikeCode(NavigationDto.class, paginationNavigation);

		model.addAttribute("navigationPagination", navigationPagination);
	}

	/**
	 * 导航：保存
	 *
	 * @param saveNavigation
	 *          导航对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final NavigationDto saveNavigation) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(NavigationService.class).saveByIdOrCode(NavigationDto.class, saveNavigation);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, NavigationController.log);
				}
			}
		};
	}
}
